package com.jinlejia.erp.adapter;

import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;

import java.util.ArrayList;


/**
 * 更多筛选第一列Adapter
 */
public class Filter1Adapter extends YfListAdapter<String> {


    private int cur_position = -1;

    public Filter1Adapter(ArrayList<String> data) {
        super(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_filter1_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);

        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(final RecyclerView.ViewHolder viewHolder, final int i) {
        ((ViewHolder)viewHolder).checkbox.setText(mData.get(i));
        if( cur_position == i ){
            ((ViewHolder)viewHolder).checkbox.setChecked(true);
            onItemSelectedListener.onItemSelected( mData.get(i) );
        }else{
            ((ViewHolder)viewHolder).checkbox.setChecked(false);
        }
        viewHolder.itemView.setTag(mData.get(i));
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(cur_position != -1){

                    notifyItemChanged(cur_position);
                }
                cur_position = i;

                notifyItemChanged(i);

            }
        });
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new YfSimpleViewHolder(view);
    }


    static final class ViewHolder extends RecyclerView.ViewHolder {
        CheckBox checkbox;
        public ViewHolder(View itemView) {
            super(itemView);
           checkbox = itemView.findViewById(R.id.checkbox);
        }
    }

    public interface OnItemSelectedListener{
       void onItemSelected(String str);
    }
    private OnItemSelectedListener onItemSelectedListener;
    public void setOnItemSelectedListener( OnItemSelectedListener listener ){
        this.onItemSelectedListener = listener;
    }


}
